<template>
  <div>
    <el-form ref="form"
             v-loading="loading"
             label-position="right"
             label-width="100px"
             :rules="rules"
             :model="form">
      <el-row>
        <el-col :span="12">
          <el-col :span="20">
            <el-form-item label="病人姓名"
                          prop="name">
              <el-input v-model="form.name"
                        placeholder="请输入病人姓名">
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-button type="primary"
                       @click="getUserInfo">搜索</el-button>
          </el-col>
        </el-col>
        <el-col :span="12">
          <el-form-item label="身份证"
                        prop="id_card">
            <el-input v-model="form.id_card"
                      placeholder="请输入身份证"
                      @blur="checkIdCard"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="年龄"
                        prop="age">
            <el-input v-model="form.age"
                      placeholder="请输入年龄"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="性别"
                        prop="sex">
            <el-select v-model="form.sex"
                       placeholder="请选择性别">
              <el-option label="男"
                         value="1"></el-option>
              <el-option label="女"
                         value="2"></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="联系电话"
                        prop="mobile">
            <el-input v-model="form.mobile"
                      placeholder="请输入联系电话"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="就诊时间"
                        prop="time">
            <el-date-picker v-model="form.time"
                            align="right"
                            type="datetime"
                            placeholder="请选择就诊时间"
                            value-format="yyyy-MM-dd HH:mm:ss">
            </el-date-picker>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="疾病选择"
                        prop="illness">
            <el-checkbox-group v-model="form.illness">
              <el-checkbox v-for="(item,index) in illnessList"
                           :key="index"
                           :label="item.id">{{ item.name }}</el-checkbox>
            </el-checkbox-group>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="医师姓名"
                        prop="doc_name">
            <el-input v-model="form.doc_name"
                      placeholder="请输入医师姓名"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="医嘱"
                        prop="msg">
            <el-input v-model="form.msg"
                      type="textarea"
                      placeholder="请输入医嘱"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="处方有效期"
                        prop="youxiao">
            <el-input v-model="form.youxiao"
                      type="textarea"
                      placeholder="请输入处方有效期"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="老人病史"
                        prop="bingshi">
            <el-input v-model="form.bingshi"
                      type="textarea"
                      placeholder="请输入老人病史"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="老人症状"
                        prop="zhengzhuang">
            <el-input v-model="form.zhengzhuang"
                      type="textarea"
                      placeholder="请输入老人症状"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="治疗方案"
                        prop="fangan">
            <el-input v-model="form.fangan"
                      type="textarea"
                      placeholder="请输入治疗方案"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="注意事项"
                        prop="zhuyi">
            <el-input v-model="form.zhuyi"
                      type="textarea"
                      placeholder="请输入注意事项"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="备注"
                        prop="note">
            <el-input v-model="form.note"
                      type="textarea"
                      placeholder="请输入备注"></el-input>
          </el-form-item>
        </el-col>

        <el-col :span="12">
          <el-form-item label="总价:">
            <span style="color: #1890ff;font-weight: bold;font-size: 16px;"> {{ totalPrice }} 元</span>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="药品名">
            <el-input v-model="drugSelect.title"
                      placeholder="请输入药品名"
                      style="width: 32%;"></el-input>
            <el-select v-model="drugSelect.m_type"
                       placeholder="请选择药品类型"
                       style="width: 32%;margin: 0 10px;">
              <el-option label="药品类型"
                         value="0"></el-option>
              <el-option v-for="(item,index) in m_arr"
                         :key="index"
                         :label="item"
                         :value="index">
              </el-option>
            </el-select>
            <el-select v-model="drugSelect.u_type"
                       placeholder="请选择使用类型"
                       style="width: 32%;">
              <el-option label="使用类型"
                         value="0"></el-option>
              <el-option v-for="(item,index) in u_arr"
                         :key="index"
                         :label="item"
                         :value="index">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-col :span="20">
            <el-form-item label="处方">
              <el-input v-model="inputValue"
                        placeholder="请输入"
                        class="input"
                        @focus="focus">
              </el-input>

            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-button type="primary"
                       @click="gerChufangInfoDetail">搜索</el-button>
          </el-col>
        </el-col>
        <el-col :span="24">
          <el-col :span="8"> <el-form-item label="药品"
                          prop="chufang">
              <el-button type="primary"
                         plain
                         @click="drugAdd">添加药品</el-button>
            </el-form-item></el-col>
          <el-col :span="8"> <el-col :span="24">
              <el-form-item prop="chufang">
                <el-button type="primary"
                           plain
                           @click="getChuFangList">导入处方</el-button>
              </el-form-item>
            </el-col></el-col>
          <el-col :span="8">
            <el-col :span="24">
              <el-form-item prop="chufang">
                <el-button type="primary"
                           plain
                           @click="addChufang">添加处方</el-button>
              </el-form-item>
            </el-col></el-col>
        </el-col>

        <div v-for="(item,index) in form.drug"
             :key="index"
             style="padding-left: 50px;display: inline-block;">
          <div style="padding-left: 50px;">
            <el-checkbox v-model="item.checked"
                         @input="inputOnInput($event)"> {{ item.title }}</el-checkbox>
            <el-button type="text"
                       style="color: #F56C6C;margin-left: 20px;"
                       @click="drugDelete(index)">删除</el-button>
          </div>
          <el-col :span="12">
            <el-form-item label="数量"
                          :prop="`drug[${index}][use_type]`"
                          :rules="rules.num">
              <el-input v-model="item.num"
                        type="number"
                        placeholder="请输入数量"
                        style="width: 80%;"
                        @input="inputOnInput($event)"
                        @change="drugNumChange"></el-input> {{ item.unit }}
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="规格:"
                          label-width="70px">
              <div style="height: 33px;"> {{ item['spec'] }}</div>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="单价:"
                          label-width="45px">
              <div style="height: 33px;"> {{ item['sell_price'] }} 元</div>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="药品类型"
                          :prop="`drug[${index}][use_type]`"
                          :rules="rules.use_type">
              <el-input v-model="item.use_type"
                        @input="inputOnInput($event)"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="服用方法"
                          :prop="`drug[${index}][func]`"
                          :rules="rules.func">
              <el-input v-model="item.func"
                        @input="inputOnInput($event)"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="每次用量"
                          :prop="`drug[${index}][use_num]`"
                          :rules="rules.use_num">
              <el-input v-model="item.use_num"
                        placeholder="片/粒/包/ml"
                        @input="inputOnInput($event)"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="次数"
                          :prop="`drug[${index}][use_times]`"
                          :rules="rules.use_times">
              <el-input v-model="item.use_times"
                        placeholder="早/中/晚"
                        @input="inputOnInput($event)"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="服药时间"
                          :prop="`drug[${index}][qianhou]`"
                          :rules="rules.qianhou">
              <el-input v-model="item.qianhou"
                        placeholder="饭前半小时"
                        @input="inputOnInput($event)"></el-input>
            </el-form-item>
          </el-col>
        </div>
      </el-row>
      <el-form-item class="contacts-add">
        <el-button type="primary"
                   @click="onSubmit">提交</el-button>
        <el-button @click="cancle">关闭</el-button>
      </el-form-item>
    </el-form>

    <!-- //搜索姓名提示 -->
    <el-dialog title="快捷添加"
               :visible.sync="dialogVisible"
               width="55%"
               :modal="false">
      <template>
        <el-table v-loading="loading"
                  :data="nameList"
                  border
                  style="width: 100%">
          <el-table-column prop="base.name"
                           label="姓名"
                           align="center">
          </el-table-column>
          <el-table-column prop="base.mobile"
                           label="手机号码"
                           align="center">
          </el-table-column>
          <el-table-column prop="base.id_card"
                           label="身份证号码"
                           align="center">
          </el-table-column>
          <el-table-column label="添加"
                           align="center">
            <template slot-scope="scope">
              <el-button type="primary"
                         @click="setFormUserInfo(scope.row)">添加</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>

    </el-dialog>
    <el-dialog title="药品信息"
               :visible.sync="dialogDrugVisible"
               :close-on-click-modal="false"
               width="700px"
               append-to-body>
      <medicine-select v-if="dialogDrugVisible"
                       :data="ruleForm"
                       @cancle="dialogDrugVisible = false"
                       @load="setSelect"></medicine-select>
    </el-dialog>
    <!-- 处方列表 -->
    <el-dialog title="处方列表"
               :visible.sync="isShowChufangDialog"
               width="70%"
               :modal="false">
      <el-table :data="formattedChufangList"
                style="width: 100%">
        <el-table-column prop="name"
                         label="名称"
                         width="180"></el-table-column>
        <el-table-column prop="doctor_name"
                         label="添加人"
                         width="180"></el-table-column>
        <el-table-column prop="create_time"
                         label="添加时间"></el-table-column>
        <el-table-column label="添加"
                         align="center">
          <template slot-scope="scope">
            <el-button type="primary"
                       @click="setChufangInfo(scope.row)">添加</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>
    <!-- //添加处方 -->
    <el-dialog title="添加处方"
               :visible.sync="isShowAddChufangDialog"
               width="70%"
               :modal="false">
      <el-form ref="form"
               v-loading="addloading"
               label-position="right"
               label-width="100px"
               :rules="addrules"
               :model="addform">
        <el-row>
          <el-col :span="12">
            <el-form-item label="处方名称"
                          prop="name">
              <el-input v-model="addform.name"
                        placeholder="请输入处方名称"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="添加人"
                          prop="doctor_name">
              <el-input v-model="addform.doctor_name"
                        placeholder="请输入添加人"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="症状描述"
                          prop="msg">
              <el-input v-model="addform.msg"
                        type="textarea"
                        placeholder="请输入症状描述"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item class="contacts-add">
          <el-button type="primary"
                     @click="addChuFangDetails">提交</el-button>
          <el-button @click="cancle">关闭</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import * as APIMedicalCare from "@/api/medicalCare";
import * as APIMedicalCareDisease from "@/api/medicalCareDisease";
import { mapState } from "vuex";
import { age, unixToDate } from "@/utils";
import * as RegExp from "@/utils/RegExp";
import medicineSelect from "../../drug/components/medicine-select";
export default {
  name: "Disease1VisitAdd",
  components: {
    medicineSelect,
  },
  props: {
    data: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    const checkValidator = (rule, value, callback) => {
      const index = rule.field
        .split("")
        .findIndex((item) => /[0-9]/.test(item));
      const obj = this.form["drug"][rule.field[index]];
      if (obj["checked"] && !obj[rule.type]) {
        callback(new Error("最大单量不能为空"));
      } else {
        callback();
      }
    };
    return {
      age: age,
      form: {},
      // 站点列表
      m_arr: {}, // 药品类型
      u_arr: {}, // 用法类型
      uf_arr: {}, // 使用方法
      drug: [],
      drugSelect: {
        title: "", // 药品名称 1
        m_type: "0", //药品类型  1
        u_type: "0", //用法类型 1
      },
      addform: {},
      addloading: false,
      addrules: {
        name: [{ required: true, message: "请输入处方名称", trigger: "blur" }],
        doctor_name: [
          { required: true, message: "请输入添加人", trigger: "blur" },
        ],
        msg: [{ required: true, message: "请输入症状描述", trigger: "blur" }],
      },
      chufangList: [],
      chufangId: "",
      dialogDrugVisible: false,
      loading: false,
      ruleForm: {},
      totalPrice: 0,
      illnessList: [],
      //搜索name的返回值和
      nameList: [],
      dialogVisible: false,
      oid: 0,
      isShowChufangDialog: false,
      isShowAddChufangDialog: false,
      // 表单校验
      rules: {
        name: [{ required: true, message: "请输入病人姓名", trigger: "blur" }],
        id_card: [{ required: true, message: "请输入身份证", trigger: "blur" }],
        illness: [{ required: true, message: "请选择疾病", trigger: "blur" }],
        doc_name: [
          { required: true, message: "请输入医师姓名", trigger: "blur" },
        ],
        chufang: [{ required: true, message: "请添加药品" }],
        num: [
          {
            required: true,
            validator: checkValidator,
            message: "请输入数量",
            type: "num",
            trigger: "blur",
          },
        ],
        use_type: [
          {
            required: true,
            validator: checkValidator,
            message: "请输入药品类型",
            type: "use_type",
            trigger: "blur",
          },
        ],
        func: [
          {
            required: true,
            validator: checkValidator,
            message: "请输入服用方法",
            type: "func",
            trigger: "blur",
          },
        ],
        use_num: [
          {
            required: true,
            validator: checkValidator,
            message: "请输入每次用量",
            type: "use_num",
            trigger: "blur",
          },
        ],
        use_times: [
          {
            required: true,
            validator: checkValidator,
            message: "请输入次数",
            type: "use_times",
            trigger: "blur",
          },
        ],
        qianhou: [
          {
            required: true,
            validator: checkValidator,
            message: "请输入服药时间",
            type: "qianhou",
            trigger: "blur",
          },
        ],
      },
      inputValue: "",
    };
  },
  computed: {
    formattedChufangList() {
      // 使用computed属性来处理时间戳并返回新的格式化数据
      return this.chufangList.map((item) => {
        const timestamp = item.create_time;
        const date = new Date(timestamp * 1000); // 将时间戳转换为JavaScript日期对象
        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, "0"); // 月份从0开始，需要加1并且补零
        const day = String(date.getDate()).padStart(2, "0"); // 补零
        const formattedDate = `${year}-${month}-${day}`;

        return {
          ...item,
          create_time: formattedDate, // 将时间戳替换为格式化后的日期字符串
        };
      });
    },
  },
  watch: {
    data: {
      handler(newval) {
        this.medicineGetarr();
        this.getIllnessIndex();
        if (newval.hasOwnProperty("id")) {
          // this.getMedicineInfo(newval['id'])
        } else {
          this.form = { ...newval, ...{ drug: [], chufang: "1", illness: [] } };
        }
      },
      // 可以一进页面就触发监听器  监听器自带的immediate
      immediate: true,
      deep: true,
    },
  },
  mounted() {
    // this.medicineGetarr()
  },
  methods: {
    medicineGetarr() {
      const self = this;
      APIMedicalCare.medicineGetarr()
        .then((resp) => {
          const res = resp.data;
          self["m_arr"] = res["m_arr"];
          self["u_arr"] = res["u_arr"];
          self["uf_arr"] = res["uf_arr"];
        })
        .finally(() => {
          this.loading = false;
        });
    },
    getIllnessIndex() {
      const self = this;
      APIMedicalCare.illnessIndex({ page_size: 1000 })
        .then((resp) => {
          if (resp.code == 200) {
            const { list = [], total = 0 } = resp.data;
            self.illnessList = list;
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    // 身份校验检测
    async checkIdCard() {
      const { id_card } = this.form;
      if (id_card === "") {
        this.$message.warning("请输入身份证号码");
        return false;
      }
      if (!RegExp.IDCard.test(id_card)) {
        // return false
      } else {
        const birthday =
          id_card.substring(6, 10) +
          "-" +
          id_card.substring(10, 12) +
          "-" +
          id_card.substring(12, 14);
        /* if (parseInt(id_card.substr(16, 1)) % 2 === 1) {
         this.form.sex = '1'
       } else {
         this.form.sex = '2'
       } */
        /* const myDate = new Date()
       const month = myDate.getMonth() + 1
       const day = myDate.getDate()
       let age = myDate.getFullYear() - cardid.substring(6, 10) - 1
       if ((cardid.substring(10, 12) < month || cardid.substring(10, 12) === month) && cardid.substring(12, 14) <= day) {
         age++
       } */
        var age = this.age(birthday, "a");
        this.form = { ...this.form, ...{ age } };
        // const exists = await APIOlder.olderFindidcard({ idcard: id_card })
        /* if (exists.data) {
         this.$message.warning('此身份证号已被使用')
       } */
      }
    },
    drugAdd() {
      this.ruleForm = { ...this.drugSelect };
      this.dialogDrugVisible = true;
    },
    drugDelete(itemIndex) {
      this.form.drug.splice(itemIndex, 1);
      this.drugNumChange();
    },
    drugNumChange() {
      const self = this;
      self.totalPrice = 0;
      self.form.drug.forEach((item) => {
        self.totalPrice += item["num"] * item["sell_price"];
      });
      // console.log(self.totalPrice)
    },
    setSelect(row) {
      const self = this;
      const filterSelect = self.form.drug.filter((num) => num.id == row.id);
      if (filterSelect.length) {
        this.$message({ type: "warning", message: "当前药品已存在!" });
        return false;
      }
      row.checked = true;
      row.num = 1; // 数量
      row["use_type"] = self["u_arr"][Number(row.u_type)]; // 药品类型
      row["func"] = self["uf_arr"][Number(row.u_func)]; // 服用方法
      row["use_num"] = ""; // 每次用量
      row["use_times"] = ""; // 次数
      row["qianhou"] = ""; // 服药时间
      self.form.drug.push(row);
      this.drugNumChange();
    },
    inputOnInput(e) {
      this.$forceUpdate();
    },
    onSubmit() {
      const self = this;
      console.log(self.form);
      const selectDrug = self.form.drug.filter((item) => item["checked"]);
      if (selectDrug.length == 0) {
        this.form["chufang"] = "";
      } else {
        this.form["chufang"] = "1";
      }
      this.$refs.form.validate((valid) => {
        if (valid) {
          const chufangForm = {
            oid: "0",
            id_card: self.form["id_card"], //病人身份证，必传
            name: self.form["name"], //病人姓名
            age: self.form["age"], //病人年龄
            sex: self.form["sex"], //病人性别
            mobile: self.form["mobile"], //病人联系电话
            time: self.form["time"], //就诊时间
            illness: self.form["illness"].join(","), //疾病选择，‘,’隔开
            doc_name: self.form["doc_name"], //医师名称
            bingshi: self.form["bingshi"], //老人病史
            zhengzhuang: self.form["zhengzhuang"], //老人症状
            fangan: self.form["fangan"], //治疗方案
            zhuyi: self.form["zhuyi"], //注意事项
            youxiao: self.form["youxiao"], //处方有效期
            note: self.form["note"], //备注
            msg: self.form["msg"], //医嘱
          };
          let objDrug = {
            code_id: [],
            code_name: [],
            code: [],
            num: [],
            unit: [],
            use_type: [],
            func: [],
            use_num: [],
            use_times: [],
            qianhou: [],
          };

          self.form.drug.forEach((item) => {
            if (item["checked"]) {
              if (item["id"]) {
                objDrug["code_id"].push(item["id"]);
              } else {
                objDrug["code_id"].push(item.code_id);
              }
              objDrug["code_name"].push(item["title"]); //药品名称
              objDrug["code"].push(item["code"]); //批准文号

              objDrug["num"].push(item["num"]); //数量
              objDrug["unit"].push(item["unit"]); //规格
              objDrug["use_type"].push(item["use_type"]); //药品类型

              objDrug["func"].push(item["func"]); //服用方法
              objDrug["use_num"].push(item["use_num"]); //每次用量
              objDrug["use_times"].push(item["use_times"]); //次数

              objDrug["qianhou"].push(item["use_times"]); //服药时间
            }
          });
          for (let objItem in objDrug) {
            chufangForm[objItem] = objDrug[objItem].join(",");
          }
          APIMedicalCareDisease.jiuzhenjiAdd(chufangForm).then((res) => {
            this.$message({ type: "success", message: "新增成功!" });
            this.cancle();
            this.load();
          });
        }
      });
    },
    load() {
      this.$emit("load");
    },
    cancle() {
      this.$refs.form.resetFields();
      this.$emit("cancle");
    },

    async getUserInfo() {
      this.loading = true;
      let userInfo = {
        name: this.form.name,
      };
      let res = await APIMedicalCareDisease.jiuZhenUserInfo(userInfo);
      this.nameList = res.data;
      this.dialogVisible = true;
      this.loading = false;
    },
    setFormUserInfo(item) {
      this.loading = true;
      this.oid = item.base.id;
      this.dialogVisible = false;
      this.form.name = item.base.name;
      this.form.id_card = item.base.id_card;
      this.form.age = item.base.age;
      this.form.mobile = item.base.mobile;
      this.form.sex = String(item.base.sex);
      this.loading = false;
    },
    gerChufangInfoDetail() {
      let params = {
        name: this.inputValue,
      };
      APIMedicalCare.chufangIndex(params).then((res) => {
        if (res.code == 200) {
          this.isShowChufangDialog = true;
          this.chufangList = res.data.list;
          console.log(res.list);
        }
      });
    },
    getChuFangList() {
      APIMedicalCare.chufangIndex().then((res) => {
        if (res.code == 200) {
          this.isShowChufangDialog = true;
          this.chufangList = res.data.list;
          console.log(res.list);
        }
      });
    },
    setChufangInfo(item) {
      this.form.drug = JSON.parse(item.note).map((drug) => ({
        ...drug,
        checked: true,
      }));
      console.log("Converted drug data:", this.form.drug);
      this.isShowChufangDialog = false;
    },
    addChufang() {
      this.isShowAddChufangDialog = true;
    },
    //添加处方
    addChuFangDetails() {
      const self = this;
      // console.log(self.form)
      const selectDrug = self.form.drug.filter((item) => item["checked"]);
      if (selectDrug.length == 0) {
        this.form["chufang"] = "";
      } else {
        this.form["chufang"] = "1";
      }
      this.$refs.form.validate((valid) => {
        if (valid) {
          const chufangForm = {
            // 'oid': '0',
            oid: this.oid,
            id_card: self.form["id_card"], //病人身份证，必传
            name: self.form["name"], //病人姓名
            age: self.form["age"], //病人年龄
            sex: self.form["sex"], //病人性别
            mobile: self.form["mobile"], //病人联系电话
            time: self.form["time"], //就诊时间
            illness: self.form["illness"].join(","), //疾病选择，‘,’隔开
            doc_name: self.form["doc_name"], //医师名称
            bingshi: self.form["bingshi"], //老人病史
            zhengzhuang: self.form["zhengzhuang"], //老人症状
            fangan: self.form["fangan"], //治疗方案
            zhuyi: self.form["zhuyi"], //注意事项
            youxiao: self.form["youxiao"], //处方有效期
            note: self.form["note"], //备注
            msg: self.form["msg"], //医嘱
          };
          console.log(chufangForm);
          let objDrug = {
            code_id: [],
            code_name: [],
            code: [],
            num: [],
            unit: [],
            use_type: [],
            func: [],
            use_num: [],
            use_times: [],
            qianhou: [],
          };

          self.form.drug.forEach((item) => {
            if (item["checked"]) {
              if (item["id"]) {
                objDrug["code_id"].push(item["id"]);
              } else {
                objDrug["code_id"].push(item.code_id);
              }
              objDrug["code_name"].push(item["title"]); //药品名称
              objDrug["code"].push(item["code"]); //批准文号

              objDrug["num"].push(item["num"]); //数量
              objDrug["unit"].push(item["unit"]); //规格
              objDrug["use_type"].push(item["use_type"]); //药品类型

              objDrug["func"].push(item["func"]); //服用方法
              objDrug["use_num"].push(item["use_num"]); //每次用量
              objDrug["use_times"].push(item["use_times"]); //次数

              objDrug["qianhou"].push(item["use_times"]); //服药时间
            }
          });
          for (let objItem in objDrug) {
            chufangForm[objItem] = objDrug[objItem].join(",");
          }
          APIMedicalCare.chufangAdd(chufangForm).then((res) => {
            if (res.code == 200) {
              this.$message({ type: "success", message: "新增成功!" });
              this.isShowAddChufangDialog = false;
              console.log("保存处方成功");
            } else {
              this.isShowAddChufangDialog = false;
            }
          });
        }
      });
    },
  },
};
</script>

<style lang="scss">
.contacts-add {
  text-align: right;
}
.contract-text {
  font-size: 12px;
  color: #999;
  line-height: 12px;
  margin: 0;
}
.contract-img {
  width: 146px;
  height: 146px;
  cursor: pointer;
}
</style>
